package com.yuneec.droneservice.connection;

import android.content.Context;
import android.os.Bundle;

import com.yuneec.droneservice.ConnectionListener;
import com.yuneec.droneservice.ServiceManager;
import com.yuneec.droneservice.protocol.ProtocolPlugin;
import com.yuneec.droneservice.protocol.analysis.AOAMonitor;
import com.yuneec.droneservice.protocol.analysis.Analysis;
import com.yuneec.droneservice.protocol.analysis.GreedyPackAnalysis;
import com.yuneec.droneservice.protocol.analysis.IAnalysis;
import com.yuneec.droneservice.protocol.analysis.IMessageReceiver;
import com.yuneec.droneservice.protocol.analysis.IdealAnalysis;

/**
 * Created by zd.zhang on 2017/3/14.
 */

public class AOAConnection implements ConnectionInterface {
    private AOAMonitor aoaMonitor;
    private Context context;
    private IAnalysis analysis;

    public AOAConnection(Context context) {
        super();
        this.context = context;
        this.analysis = new IdealAnalysis(100, 8192);
//        this.analysis = new GreedyPackAnalysis();
    }

    public boolean open(Bundle parameter, ConnectionListener listener) {
        if (aoaMonitor == null) {
            aoaMonitor = new AOAMonitor(context, analysis);
            aoaMonitor.setConnectListener(listener);
            aoaMonitor.start();
        }
        return true;
    }

    @Override
    public void close() {
        if (aoaMonitor != null) {
            aoaMonitor.close();
            aoaMonitor = null;
        }
    }


    public void registReceiver(IMessageReceiver receiver) {
        analysis.registMessageHandler(receiver);
    }


    public void unRegistReceiver(int id) {
        analysis.unRegistMessageHandler(id);
    }


    public int writeDataBlock(int id, byte[] data) {
        return analysis.send(id, data);
    }

    public void writeData(int id, byte[] data) {
        analysis.asyncSend(id, data);
    }

    //    private static final int BASE_COMMAND_MSG_ID = 1108;
    private DataReceivedListener dataReceivedListener;

    @Override
    public void setReceiveListener(final DataReceivedListener listener) {
        ProtocolPlugin plugin = ServiceManager.getInstance().getProtocolPlugin();
        int port = plugin == null ? -1 : plugin.getPort();
        if (port <= 0) {
            return;
        }
        if (listener != null) {
            this.dataReceivedListener = listener;
            registReceiver(new IMessageReceiver.IDMessageReceiver(port) {

                @Override
                public void handleMessage(Analysis.Message msg) {
                    if (dataReceivedListener != null) {
                        dataReceivedListener.onReceive(msg.body(), msg.offset(), msg.length());
                    }
                }

                @Override
                public int getMessageId() {
                    return msgId;
                }

                @Override
                public void catchException(Throwable e) {

                }
            });
        } else {
//            unRegistReceiver(BASE_COMMAND_MSG_ID);
            unRegistReceiver(port);
            this.dataReceivedListener = null;
        }
    }

}
